unit AnoymousThreads;

interface
uses
  Windows, Classes, SysUtils, StrUtils, System.Threading;
type
  TAnonymousThreadSingle<T> = class(TThread)
  private
    FProc: TProc<T>;
    AValue:T;
  protected
    procedure Execute; override;
  public
    constructor Create(const AProc: TProc<T>;ProcPar:T);
  end;

  TAnonymousThreadDouble<T,T1> = class(TThread)
  private
    FProc: TProc<T,T1>;
    AValue:T;
    BValue:T1;
  protected
    procedure Execute; override;
  public
    constructor Create(const AProc: TProc<T,T1>;ProcPar:T;ProcParB:T1);
  end;

  TThreadHelper= class helper for TThread
    public
      class function CreateAnonymousThreadSingle<T>(const ThreadProc: TProc<T>;proPar:T): TThread; static;
      class function CreateAnonymousThreadDouble<T,T1>(const ThreadProc: TProc<T,T1>;proPar:T;proParB:T1): TThread; static;
  end;

implementation

{ TAnonymousThreadX }

constructor TAnonymousThreadSingle<T>.Create(const AProc: TProc<T>;ProcPar:T);
begin
  inherited Create(True);
  FreeOnTerminate := True;
  FProc := AProc;
  Avalue:=ProcPar;
end;

procedure TAnonymousThreadSingle<T>.Execute;
begin
  inherited;
  FProc(Avalue);
end;

constructor TAnonymousThreadDouble<T,T1>.Create(const AProc: TProc<T,T1>;ProcPar:T;ProcParB:T1);
begin
  inherited Create(True);
  FreeOnTerminate := True;
  FProc := AProc;
  Avalue:=ProcPar;
  BValue:=ProcParB;
end;

procedure TAnonymousThreadDouble<T,T1>.Execute;
begin
  inherited;
  FProc(Avalue,BValue);
end;


{ TThreadHelper }

class function TThreadHelper.CreateAnonymousThreadSingle<T>(const ThreadProc: TProc<T>; proPar: T): TThread;
begin
  Result := TAnonymousThreadSingle<T>.Create(ThreadProc,proPar);
end;

class function TThreadHelper.CreateAnonymousThreadDouble<T,T1>(const ThreadProc: TProc<T,T1>; proPar: T;proParB: T1): TThread;
begin
  Result := TAnonymousThreadDouble<T,T1>.Create(ThreadProc,proPar,proParB);
end;

end.
